import { loginApi,captchaApi } from '../http/userApi'


export default{
    namespaced: true,
    state: {
        username:localStorage.getItem('username'),
        // userId:localStorage.getItem('userId'),
        token:localStorage.getItem('token'),
        routes:JSON.parse(localStorage.getItem('routes') || '[]'),
        initRoutes:[]//存储处理之后的路由数据
    },
    getters: {
    },
    mutations: {
        loginMutation(state, user){// 改变state里的值
            state.username = user.username;
            // state.userId = user.userId;
            state.token = user.token;
            // console.log(state.username,state.token);
            // console.log(state.userId,state.username,state.token);
        },
        logoutMutation(state){
            state.username = null;
            // state.userId = null;
            state.token = null;
            state.routes = [];
            state.initRoutes = [];
        },
        setInitRoutes(state, routes) {
            state.initRoutes = routes
        }
    },
    actions: {
        captchaAction(obj, captcha){
            
            return new Promise((resolve,reject)=>{
                console.log(captcha.phone);
                
                captchaApi(captcha.phone)
                .then((res)=>{
                    console.log(res.data);
                    resolve();
                },(err)=>{
                    reject(err);
                })
            })
        },

        loginAction({commit},user){
            console.log(user.username,user.password,user.captcha);
            return new Promise((resolve,reject) => {
                loginApi(user.username,user.password,user.captcha)
                .then((res)=>{
                    
                    console.log(res.data);
                    if(res.data.code == "1"){
                        commit('loginMutation',{//需要传userId、username、token的值
                        // userId:res.data.data.id,
                        username:user.username,
                        token:res.data.token,
                        });
                    }
                    // 路由数据-数组
                    let routes = res.data.data;
                    // 为数组添加一个404界面 
                    routes.push({
                        path:'*',redirect: '/404',hidden:0
                    })

                    // commit('setRoutes',routes);

                    console.log(res.data.token);
                    // 将登录信息存到本地存储 
                    // localStorage.setItem('userId',res.data.data.id);
                    localStorage.setItem('username',user.username);
                    localStorage.setItem('token',res.data.token);
                    localStorage.setItem('routes',JSON.stringify(res.data.data));
                    
                    resolve();
                },(err)=>{
                    reject(err);
                })
               
            })
        },
        logoutAction({commit}){
            return new Promise((resolve)=>{
                commit('logoutMutation');
                localStorage.clear();
                resolve();
            })
        }
    },
}